<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="店铺名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <dict-select :options="dict.type.store_flow_status" v-model="queryParams.status" placeholder="请选择状态"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="statisticsList">
      <el-table-column label="店铺名称" align="center" prop="shopName" width="180" :show-overflow-tooltip="true"/>
      <el-table-column label="最后使用时间" align="center" prop="lastUseTime" width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUseTime, '{m}/{d}/{y}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后购买时间" align="center" prop="lastBuyTime" width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastBuyTime, '{m}/{d}/{y}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总小时" align="center" prop="totalQuantity"/>
      <el-table-column label="剩余小时" align="center" prop="residue"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.store_flow_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['car:statistics:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['car:statistics:remove']"
            >删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listStatistics, getStatistics, delStatistics, addStatistics, updateStatistics } from '@/api/store/storeFlow'
import Template from '@/views/setting/template/index.vue'

export default {
  name: 'Statistics',
  components: { Template },
  dicts: ['store_flow_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 店铺流量剩余统计表格数据
      statisticsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询店铺流量剩余统计列表 */
    getList() {
      this.loading = true
      listStatistics(this.queryParams).then(response => {
        this.statisticsList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shopName: null,
        shopId: null,
        lastUseTime: null,
        lastBuyTime: null,
        totalQuantity: null,
        residue: null,
        status: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const h = this.$createElement
      this.$prompt(h('p', null, [h('i', { style: 'color: red' }, '* '),
        h('span', null, '剩余小时数修改')]), '流量包修改', {
        confirmButtonText: this.$t('ok'),
        cancelButtonText: this.$t('cancel'),
        closeOnClickModal: false,
        inputType: 'el-input-number',
        inputPattern: /^[+]?(0|([1-9]\d*))(\.\d+)?$/,
        inputErrorMessage: '请输入正确的数字'
      }).then(async({ value }) => {
        await updateStatistics(row.id, value)
        this.$modal.msgSuccess('修改成功')
        this.getList()
      }).catch(() => {
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除店铺流量剩余统计编号为"' + ids + '"的数据项？').then(function() {
        return delStatistics(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
